package com.youhong.teethcare.settingpage;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.youhong.teethcare.BaseActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.adapter.WeekendAdapter;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.ToastUtil;
import com.youhong.teethcare.utils.Util;

import java.util.ArrayList;
import java.util.List;

public class NotificationSettingActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_setting);
        notificationId = getIntent().getIntExtra("id", 1);
        getViews();

    }

    ImageView iv_back;
    TextView tv_save;

    TimePicker tp;
    ListView lv;

    int notificationId;
    String time;

    WeekendAdapter adapter;

    private void getViews() {

        iv_back = findViewById(R.id.personalInfo_iv_back);
        iv_back.setOnClickListener(this);

        tv_save = findViewById(R.id.notificationSetting_tv_save);
        tv_save.setOnClickListener(this);

        tp = findViewById(R.id.notificationSetting_timepicker);
        //设置TimePicker 24小时显示模式
        tp.setIs24HourView(true);


        lv = findViewById(R.id.notificationSetting_lv);

        //初始周选择

        ArrayList<Integer> positons = new ArrayList<>();

        if (notificationId == 1) {

            positons = setDaysOfWeek(Common.userInfo.getClockDayOfWeek1());
            time = Common.userInfo.getClockTime1();


        } else if (notificationId == 2) {

            positons = setDaysOfWeek(Common.userInfo.getClockDayOfWeek2());
            time = Common.userInfo.getClockTime2();

        } else if (notificationId == 3) {

            positons = setDaysOfWeek(Common.userInfo.getClockDayOfWeek3());
            time = Common.userInfo.getClockTime3();

        } else if (notificationId == 4) {

            positons = setDaysOfWeek(Common.userInfo.getClockDayOfWeek4());
            time = Common.userInfo.getClockTime4();

        }
        adapter = new WeekendAdapter(getApplicationContext(), positons);
        lv.setAdapter(adapter);


        int hours = Integer.parseInt(time.substring(0, 2));
        int minutes = Integer.parseInt(time.substring(3));

        tp.setCurrentHour(hours);
        tp.setCurrentMinute(minutes);
    }

    //bit-0 : Sun , bit-1:Mon, bit-2:Tue so on...

    private int getDaysOfWeek() {

        int weeks = 0;
        ArrayList<Integer> list = adapter.getCheckItem();
        for (int day : list) {

            int val = 0x1;
            val <<= day;
            weeks += val;

        }
        Common.userInfo.setClockDayOfWeek1(weeks);
        Common.userInfo.SyncToLocal(getApplicationContext());
        return weeks;
    }

    private ArrayList<Integer> setDaysOfWeek(int daysOfWeek) {

        ArrayList<Integer> arrayList = new ArrayList();
        if ((daysOfWeek & 0x1) != 0) {

            arrayList.add(0);

        }

        if ((daysOfWeek & 0x2) != 0) {

            arrayList.add(1);


        }
        if ((daysOfWeek & 0x4) != 0) {

            arrayList.add(2);

        }
        if ((daysOfWeek & 0x8) != 0) {

            arrayList.add(3);

        }

        if ((daysOfWeek & 0x10) != 0) {

            arrayList.add(4);

        }

        if ((daysOfWeek & 0x20) != 0) {

            arrayList.add(5);

        }

        if ((daysOfWeek & 0x40) != 0) {

            arrayList.add(6);
        }
        return arrayList;
    }

    @Override
    public void onClick(View v) {

        if (iv_back == v) {
            this.finish();
        } else if (v == tv_save) {

            if (notificationId == 1) {

                Common.userInfo.setClockTime1(Util.concateZero(tp.getCurrentHour())
                        + ":" + Util.concateZero(tp.getCurrentMinute()));

                Common.userInfo.setClockDayOfWeek1(getDaysOfWeek());

                if (getDaysOfWeek() == 0) {
                    ToastUtil.showShortToast(this, R.string.strings70);
                    return;
                }
                this.finish();


            } else if (notificationId == 2) {

                Common.userInfo.setClockTime2(Util.concateZero(tp.getCurrentHour())
                        + ":" + Util.concateZero(tp.getCurrentMinute()));

                Common.userInfo.setClockDayOfWeek2(getDaysOfWeek());

                if (getDaysOfWeek() == 0) {
                    ToastUtil.showShortToast(this, R.string.strings70);
                    return;
                }

                this.finish();
            } else if (notificationId == 3) {

                Common.userInfo.setClockTime3(Util.concateZero(tp.getCurrentHour())
                        + ":" + Util.concateZero(tp.getCurrentMinute()));

                Common.userInfo.setClockDayOfWeek3(getDaysOfWeek());

                if (getDaysOfWeek() == 0) {
                    ToastUtil.showShortToast(this, R.string.strings70);
                    return;
                }
                this.finish();
            } else if (notificationId == 4) {

                Common.userInfo.setClockTime4(Util.concateZero(tp.getCurrentHour())
                        + ":" + Util.concateZero(tp.getCurrentMinute()));

                Common.userInfo.setClockDayOfWeek4(getDaysOfWeek());
                if (getDaysOfWeek() == 0) {
                    ToastUtil.showShortToast(this, R.string.strings70);
                    return;
                }
                this.finish();
            }
        }

    }
}
